package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.db.DictionaryHelper;
import com.yksj.healthtalk.ui.R;
/**
 * 专家解答 非医疗分类查询页面
 * @author Administrator
 *
 */
public class ServerCenterSavantSeachListActivity extends BaseFragmentActivity implements OnClickListener, OnItemClickListener {

	private ListView mListView;
	private List<String> entities;
	private MyAdapter adapter;
	private DictionaryHelper mDictionaryHelper;
	private LinkedHashMap<String, String> keshiData = new LinkedHashMap<String, String>();
	private LodingFragmentDialog showLodingDialog;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.server_center_savant_seach_list);
		showLodingDialog = LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(), "请稍后...");
		initTitle();
		initView();
		initData();
	}


	private void initData() {
		mDictionaryHelper = DictionaryHelper.getInstance(this);
		new Thread(){
			public void run() {
				keshiData = mDictionaryHelper.queryKeShi(getBaseContext());
				ArrayList<String> arr = new ArrayList<String>(keshiData.keySet());
				entities.addAll(arr);
				entities.add(0,"全部");
				arr=null;
				ServerCenterSavantSeachListActivity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						adapter.notifyDataSetChanged();
						showLodingDialog.dismissAllowingStateLoss();
					}
				});
				
			};
		}.start();
	}

	private void initView() {
		
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("分类查询");
		titleTextV.setTextColor(Color.BLACK);
		titleRightBtn2.setVisibility(View.GONE);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setBackgroundResource(R.drawable.banner_red);
		titleRightBtn2.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.listview);
		entities=new ArrayList<String>();
		adapter = new MyAdapter(entities,ServerCenterSavantSeachListActivity.this);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
	}

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
			break;
		}
	}
	
	class MyAdapter extends BaseAdapter{

		private final List<String> entities;
		private final LayoutInflater inflater;
		
		public MyAdapter(List<String> entities, Context mContext) {
			this.entities = entities;
			inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			return entities.size();
		}

		@Override
		public Object getItem(int position) {
			return entities.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView=inflater.inflate(R.layout.seach_item_text, null);
			}
			TextView textView=(TextView) convertView.findViewById(R.id.title_lable);
			ImageView view=(ImageView) convertView.findViewById(R.id.icon_img);
			view.setVisibility(View.GONE);
			textView.setText(entities.get(position));
			if(position==0) {
				convertView.setBackgroundResource(R.drawable.textview_background_up);
			} else if(position== getCount()-1) {
				convertView.setBackgroundResource(R.drawable.textview_background_down);
			} else {
				convertView.setBackgroundResource(R.drawable.textview_background_middle);
			}
			return convertView;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String datnum=keshiData.get(entities.get(position));
		Intent intent = getIntent();
		if(datnum!=null)
		intent.putExtra("classID", datnum);
		setResult(RESULT_OK, intent);
		ServerCenterSavantSeachListActivity.this.finish();
	}
}
